package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fechas.DSIMysql;

import validaciones.ValidacionData;
import validaciones.ValidateJSON;

/**
 * Servlet implementation class PorcentajeMes
 */
@WebServlet("/PorcentajeMes")
public class PorcentajeMes extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PorcentajeMes() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		JSONArray Items=new JSONArray();
		JSONArray item=new JSONArray();
		JSONObject j=new JSONObject();
		
		ValidacionData validacion=new ValidacionData();
		DSIMysql dsi=new DSIMysql();	
		
		String id_usuario=request.getParameter("carnet");
		String mes=request.getParameter("mes");
		String tipo=request.getParameter("tipo");
		
			
			String porcentaje=dsi.Porcentaje_Mes(id_usuario, Integer.parseInt(mes));
			
			item.put(id_usuario);
			item.put(porcentaje);
			Items.put(item);
			
			
			try {
				if(porcentaje.length()>0){
					j.put("resultado", "10");//significa que es correcto	
				}
	
				j.put("aDataSet", Items);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		
		
		PrintWriter out = response.getWriter();		
		response.setContentType("application/json");
		out.print(j);
		out.close();
	}

}
